import './interceptor/promisifyAdaptor';
import './interceptor/routeInterceptor';
import uvRequeest from './uv/uvRequeest';

export const downloadAndOpen = (url: string) => {
    if (url) {
        // @ts-ignore
        uni.$uv.http.download(url).then(resp => {
            if (resp.header['Content-Type'].includes('image/')) {
                uni.previewImage({
                    urls: [resp.tempFilePath],
                    current: 0,
                    success: function (res) {
                        console.log('图片预览成功');
                    },
                    fail: function (err) {
                        console.error('图片预览失败: ', err);
                    }
                });
            } else {
                //新开页面打开文档，支持格式：doc, xls, ppt, pdf, docx, xlsx, pptx。
                uni.openDocument({
                    filePath: resp.tempFilePath,
                    showMenu: true,
                    success: function (res) {
                        console.log('打开文档成功');
                    }
                });
            }

        }).catch(err => {
            console.log(err)
        })
    }
}

export const getFileNameByUrl = (url: string) => {
    return url.split('/').pop();
}

export default uvRequeest;
